package com.etc.service.impl;

import com.etc.dao.CollectionsMapper;
import com.etc.entity.Collections;
import com.etc.service.CollectionsService;


import com.etc.util.PageData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
@Service(value = "collectionService")
@Mapper
public class CollectionsServiceImpl implements CollectionsService {
    @Resource(name="collectionsMapper")
    private CollectionsMapper collectionsMapper;
    @Override
    public boolean insert(Collections record) {
        return collectionsMapper.insert(record)>0;
    }

    @Override
    public boolean deleteByPrimaryKey( Integer uid, Integer tid) {
        return collectionsMapper.deleteByPrimaryKey(uid, tid)>0;
    }

    @Override
    public HashMap updateCollections(Integer uid, Integer tid) {
        HashMap map = new HashMap();
        Collections ct=new Collections();
        ct.setuId(uid);
        ct.settId(tid);

        List list=collectionsMapper.selectCollections(uid,tid);
        //判断是新增还是删除

            if(list.size()==0){
                int result=collectionsMapper.insert(ct);
              if(result>0) {
             map.put("result", "添加收藏成功");
              }else {
                  map.put("result", "添加收藏失败");
                  }

            }else if(list.size()!=0){
            int result= collectionsMapper.deleteByPrimaryKey(uid, tid);
            if(result>0) {
                map.put("result", "取消收藏成功");
            }else {
                map.put("result", "取消收藏失败");
            }
        }
        return map;
    }

    @Override
    public PageData<Collections> selectCollectionByPage(int page, int pageSize, String content) {

        // 之前的业务判断，此时在这里完成
        int start = (page - 1) * pageSize;

        // 查询需要返回的核心数据[指定页码的]
        List<Collections> data = collectionsMapper.selectCollectionByPage(start, pageSize, content);
        // 查询满足条件的记录数
        int count = collectionsMapper.selectCount(content);
        PageData<Collections> pd = new PageData<>(data, count, pageSize, page);
        return pd;
    }



}
